package com.xiezc.action;

import com.alibaba.fastjson.JSON;
import com.xiaoleilu.hutool.lang.Assert;
import com.xiezc.dto.RespData;

import com.xiezc.entity.FileInfo;
import com.xiezc.util.RetrofitUtil;
import com.xiezc.util.TBPanException;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.springframework.stereotype.Component;
import retrofit2.Call;
import retrofit2.http.*;


import java.io.IOException;
import java.util.List;

/**
 * Created by wb-xzc291800 on 2017/6/22.
 */
@Component
public class FileInfoAction {

    FileInfoInterface fileInfoInterface = RetrofitUtil.create(FileInfoInterface.class);

    public RespData findFileInfos(String ticket) throws IOException {
        Assert.notBlank(ticket);
        Call<RespData> respDataCall = fileInfoInterface.findFileInfos(ticket);
        RespData respData = respDataCall.execute().body();
        if (respData.getCode() == 200) {
            return respData;
        } else {
            throw new TBPanException(respData);
        }

    }

    public RespData addFileInfos(String ticket, List<FileInfo> fileInfos) throws IOException {
        Assert.notBlank(ticket);
        Assert.notEmpty(fileInfos);

        RequestBody requestBody = RequestBody
                .create(
                        MediaType.parse("application/json; charset=utf-8"),
                        JSON.toJSON(fileInfos).toString()
                );
        Call<RespData> respDataCall = fileInfoInterface.addFileInfos(ticket, requestBody);
        RespData respData = respDataCall.execute().body();
        if (respData.getCode() == 200) {
            return respData;
        } else {
            throw new TBPanException(respData);
        }
    }

    public RespData deleteFileInfos(String ticket, List<FileInfo> fileInfos) throws IOException {
        Assert.notBlank(ticket);
        Assert.notEmpty(fileInfos);
        RequestBody requestBody = RequestBody
                .create(
                        MediaType.parse("application/json; charset=utf-8"),
                        JSON.toJSON(fileInfos).toString()
                );
        Call<RespData> respDataCall = fileInfoInterface.deleteFileInfos(ticket, requestBody);
        RespData respData = respDataCall.execute().body();
        if (respData.getCode() == 200) {
            return respData;
        } else {
            throw new TBPanException(respData);
        }
    }


    public RespData updatePath(String ticket, String newPath) throws IOException {
        Assert.notBlank(ticket);
        Assert.notEmpty(newPath);

        Call<RespData> respDataCall = fileInfoInterface.updatePath(ticket, newPath);
        RespData respData = respDataCall.execute().body();
        if (respData.getCode() == 200) {
            return respData;
        } else {
            throw new TBPanException(respData);
        }
    }

    public interface FileInfoInterface {


        @FormUrlEncoded
        @POST("fileInfo/updatePath")
        Call<RespData> updatePath(@Header("ticket") String ticket, @Field("newPath") String newPath);

        @POST("fileInfo/findFileInfos")
        Call<RespData> findFileInfos(@Header("ticket") String ticket);

        @Headers({"Content-Type: application/json", "Accept: application/json"})//需要添加头
        @POST("fileInfo/addFileInfos")
        Call<RespData> addFileInfos(@Header("ticket") String ticket, @Body RequestBody requestBody);

        @Headers({"Content-Type: application/json", "Accept: application/json"})//需要添加头
        @POST("fileInfo/deleteFileInfos")
        Call<RespData> deleteFileInfos(@Header("ticket") String ticket, @Body RequestBody requestBody);
    }
}
